/**
 * 
 */
package com.dfhc.tag;

import java.util.Stack;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;

import org.apache.commons.mvc.bean.MvcException;

import com.dfhc.ISystemConstant;
import com.dfhc.util.StringHelper;

/**
 * @author longsebo
 * 区域标签，用于布局区域定位
 */
public class FrameworkRegionTag extends BaseTag {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2489881700460871318L;
	/**
	 * 区域名称 
	 * 
	 */
	private String name;
	
	public int doStartTag() throws JspException {
		return EVAL_BODY_BUFFERED;

	}

	public void setBodyContent(BodyContent b) {
		super.setBodyContent(b);
	}

	public void doInitBody() throws JspException {
		super.doInitBody();
	}

	public int doAfterBody() throws JspException {
		return 6;
	}


	public int doEndTag() throws JspException {
		try {			
			if(StringHelper.isEmpty(name)){
				throw new MvcException("区域名称不能为空!");
			}
			BodyContent body = getBodyContent();
			//从session取出模块内容(可能已经加工过部分)
			Stack<String>   stack=FrameworkTemplateTag.currentStack();
			String  info=stack.pop();

			if(StringHelper.isEmpty(info)){
				throw new MvcException("模板内容为空或者已经出错!");
			}
			//获取区域对应签名
			String sign = super.getTemplateRegionSign(name);
			if (info.indexOf(sign) == -1) {
				throw new MvcException("模板文件不包含区域名称:"+name);
			}
			if (body == null) {
				throw new MvcException("区域包含内容为空!");
			}
			String content = info.replace(sign, body.getString());		
			//替换后，重新放入session，下一个区域继续替换
			stack.push(content);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
